import 'package:flutter/material.dart';
import 'package:icooker/configs/mock_datas.dart';
import 'package:icooker/model/reommend_data.dart';
import 'package:video_player/video_player.dart';

/// 视频播放
class JCVideoPlayer extends StatefulWidget {
  VideoInfoData videoInfo;

  JCVideoPlayer(this.videoInfo, {Key? key}) : super(key: key);

  @override
  _JCVideoPlayerState createState() => _JCVideoPlayerState();
}

class _JCVideoPlayerState extends State<JCVideoPlayer> {
  late VideoPlayerController _videoPlayerController;
  late Future<void> _initPlayerFuture;

  @override
  void initState() {
    var videoUrl = widget.videoInfo.video!.vendorVideo;
    var dataSource = URL_HTTP_VIDEOS[1];
    print("dataSource: $dataSource");
    _videoPlayerController = VideoPlayerController.network(dataSource);
    _initPlayerFuture = _videoPlayerController.initialize();
    // 循环播放
    _videoPlayerController.setLooping(true);
    super.initState();
  }

  doPlay() async {
    if (_videoPlayerController.value.isPlaying) {
      _videoPlayerController.pause();
    }
    Future.delayed(Duration(milliseconds: 300)).then((value) {
      _videoPlayerController.play();
    });
  }

  @override
  void dispose() {
    super.dispose();
    _videoPlayerController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.videoInfo.title!),
      ),
      body: FutureBuilder(
        future: _initPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return AspectRatio(
              aspectRatio: _videoPlayerController.value.aspectRatio,
              child: VideoPlayer(_videoPlayerController),
            );
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            if (_videoPlayerController.value.isPlaying) {
              _videoPlayerController.pause();
            } else {
              _videoPlayerController.play();
            }
          });
        },
        child: Icon(_videoPlayerController.value.isPlaying
            ? Icons.pause
            : Icons.play_arrow),
      ),
    );
  }
}
